package com.song.service.imlp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.mapper.EmpLogMapper;
import com.song.mapper.EmpMapper;
import com.song.pojo.*;
import com.song.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpLogMapper empLogMapper;

    public EmpServiceImpl(EmpMapper empMapper) {
        this.empMapper = empMapper;
    }



//    @Override
//    public PageResult pagelist(Integer index, Integer size) {
//        PageResult<Emp> pageResult = new PageResult<Emp>();
//        pageResult.setTotal(empMapper.count());
//        pageResult.setRows(empMapper.list(index,size));
//        return pageResult;
//    }
    @Override
    public PageResult pagelist(EmpQueryParam  param) {
        PageHelper.startPage(param.getPage(),param.getSize());
        List<Emp> empList = empMapper.list(param);
        Page<Emp> empPage = (Page<Emp>) empList;
        return new PageResult<Emp>(empPage.getTotal(),empPage.getResult());
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class})
    public void addEmp(Emp emp) throws Exception {
        System.out.println(emp);
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.addEmp(emp);
            List<EmpExpr> empExprList = emp.getExprList();
            if(!empExprList.isEmpty()){
                for (EmpExpr empExpr : empExprList) {
                    empExpr.setEmpId(emp.getId());
                    empExpr.setCreateTime(LocalDateTime.now());
                    empExpr.setUpdateTime(LocalDateTime.now());
                }
                empMapper.addEmpExpr(empExprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "添加员工" + emp);
            empLogMapper.addEmpLog(empLog);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empMapper.deleteEmpExprByIds(ids);
    }

    @Override
    public Emp findById(Integer id) {
      return empMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
        empMapper.deleteEmpExprByIds(Arrays.asList(emp.getId()));
        List<EmpExpr> empExprList = emp.getExprList();
        if(!empExprList.isEmpty()){
            for (EmpExpr empExpr : empExprList) {
                empExpr.setUpdateTime(LocalDateTime.now());
                empExpr.setCreateTime(LocalDateTime.now());
                empExpr.setEmpId(emp.getId());
            }
            empMapper.addEmpExpr(empExprList);
        }


    }

    @Override
    public LoginInfo login(Emp emp) {

        Emp e = empMapper.findByUsernameAndPassword(emp);
        if(e != null){
            log.info("登录成功{}",e);
                return new LoginInfo(e.getId(),e.getUsername(),e.getName(),"");
        }
        return null;
    }
}
